Skip to content

test(ContainerResource): unit coverage for RestartPolicy + ContainerCreateOptions Codable#1

Open
chrisgeo wants to merge 1 commit into
JaewonHur:container-restartfrom
full-chaos:tests/restart-policy-codable
Open

test(ContainerResource): unit coverage for RestartPolicy + ContainerCreateOptions Codable#1
chrisgeo wants to merge 1 commit into
JaewonHur:container-restartfrom
full-chaos:tests/restart-policy-codable

Conversation

@chrisgeo

Copy link
Copy Markdown

Summary

Adds unit-level test coverage for RestartPolicy and ContainerCreateOptions Codable behavior, complementing the existing TestCLIRunRestart end-to-end integration tests in this PR.

Why this is useful

TestCLIRunRestart exercises full restart-policy behavior against a live daemon — which is the right end-to-end coverage. But several wire-shape invariants are easier to lock down with fast, daemon-free unit tests:

  • decodesLegacyOptionsWithoutRestartPolicy — the most important test. ContainerCreateOptions.init(from:) uses decodeIfPresent ?? .no so older options.json blobs (written by daemon versions that predate this PR) still decode cleanly. Removing that default in a future refactor would silently break every existing container's options file on disk. This test makes that contract explicit.
  • rejectsUnknownString — Compose-spec includes unless-stopped; this PR deliberately does not. The test pins that decision: any tool emitting the broader Docker set fails loudly with a DecodingError instead of silently degrading. Important for downstream consumers like container-compose that may emit the broader set.
  • encodesAsBareString / encodedJSONIncludesRestartPolicyField — pin the JSON shape so a future contributor can't accidentally regress the bare-string encoding or rename CodingKeys.
  • roundTripPreservesRestartPolicy / decodesEveryCase / defaultStaticIsNoRestart — standard hygiene.

Files

  • Tests/ContainerResourceTests/RestartPolicyTests.swift (new, 107 LoC, 8 tests)

No production code touched.

Verification

swift build                                      -> Build complete! (69.11s)
swift test --filter RestartPolicyTests           -> 8 tests passed in 0.001s

Apple silicon, macOS 26.

Context

I'm tracking this work from a fork that adopted the SDK shape from this PR (full-chaos#13) and wrote the equivalent unit tests there. After the audit pivot to align the fork with this upstream PR, the tests transfer 1:1. Filing them here directly per your "if you make PR to my branch, let me merge it" invitation upthread. Happy to revise or split if any of these test the wrong layer.

Refs apple#1258, apple#286.

…reateOptions Codable

TestCLIRunRestart already covers end-to-end behavior against a live
daemon. These add a complementary unit-level layer over the wire shape
of 'RestartPolicy' and the forward-compatibility guarantees of
'ContainerCreateOptions':

  - encodesAsBareString — '.always' -> "always" (catches accidental
    enum-with-associated-value encoding regressions).
  - decodesEveryCase — round-trip safety for { no, onFailure, always }.
  - rejectsUnknownString — Compose-spec mode 'unless-stopped' is
    intentionally NOT in this PR's enum. Test pins that decision: any
    downstream tool that emits the broader Docker set fails loudly
    rather than silently degrading.
  - decodesLegacyOptionsWithoutRestartPolicy — the wire-compat
    invariant. An 'options.json' blob written by a daemon that
    predates 'restartPolicy' must still decode, defaulting to '.no'.
    This locks in the 'decodeIfPresent ?? .no' contract in
    ContainerCreateOptions.init(from:) — removing it would silently
    break every existing container on disk.
  - decodesLegacyOptionsWithAutoRemove — same invariant with a
    non-zero pre-existing field present, ensuring the default doesn't
    get triggered by unrelated keys.
  - roundTripPreservesRestartPolicy — basic options-level round-trip.
  - encodedJSONIncludesRestartPolicyField — confirms the field name
    matches the CodingKeys declaration (catches typo regressions in
    the key enum).
  - defaultStaticIsNoRestart — pins '.default' to '.no'.

No production code touched. Verification:

  swift build       -> Build complete! (69.11s), exit 0.
  swift test --filter RestartPolicyTests
                    -> 8/8 tests passed in 0.001s.

Refs apple#1258.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant